//
//  UUNotificationNewsCell.m
//  UUCharterSupplier
//
//  Created by ZhangAo on 06/03/2018.
//  Copyright © 2018 UUCharter. All rights reserved.
//

#import "UUNotificationNewsCell.h"
#import <UUFoundation/UUFileManager.h>

@interface UUNotificationNewsCell ()

@property (nonatomic, strong) UILabel *titleLabel;
@property (nonatomic, strong) UIImageView *contentImageView;
@property (nonatomic, strong) UILabel *descLabel;

@end

@implementation UUNotificationNewsCell

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        self.titleLabel = [UILabel new];
        self.titleLabel.font = [UIFont systemFontOfSize:16];
        self.titleLabel.textColor = COMMON_TEXT_BLACK_COLOR;
        [self.containerContentView addSubview:self.titleLabel];
        [self.titleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.right.mas_equalTo(0);
            make.top.mas_equalTo(22);
        }];
        
        self.contentImageView = [UIImageView new];
        self.contentImageView.contentMode = UIViewContentModeScaleAspectFit;
        [self.containerContentView addSubview:self.contentImageView];
        
        self.descLabel = [UILabel new];
        self.descLabel.font = [UIFont systemFontOfSize:14];
        self.descLabel.textColor = COMMON_TEXT_BLACK_COLOR;
        self.descLabel.numberOfLines = 2;
        [self.containerContentView addSubview:self.descLabel];
        [self.descLabel mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.right.equalTo(self.titleLabel);
            make.top.equalTo(self.contentImageView.mas_bottom).offset(12);
            make.bottom.mas_equalTo(0);
        }];
    }
    return self;
}

- (void)setupWithModel:(UUNotificationModel *)model {
    [super setupWithModel:model];
    
    UUNotificationContentPlatformModel *content = (UUNotificationContentPlatformModel *)model.content;
    self.titleLabel.text = content.title;
    self.descLabel.text = content.desc;
    
    NSString *image = content.img;
    if (image == nil) {
        self.contentImageView.image = nil;
        
        [self.contentImageView mas_remakeConstraints:^(MASConstraintMaker *make) {
            make.left.right.mas_equalTo(0);
            make.top.equalTo(self.titleLabel.mas_bottom);
            make.height.mas_equalTo(0);
        }];
    } else {
        [self.contentImageView mas_remakeConstraints:^(MASConstraintMaker *make) {
            make.left.right.mas_equalTo(0);
            make.top.equalTo(self.titleLabel.mas_bottom).offset(11);
            make.height.equalTo(self.contentImageView.mas_width).multipliedBy(0.4);
        }];
        
        NSString *imageURL = [GetFileManager() generateRemoteImageURLWithKey:image];
        [self.contentImageView uu_setImageWithURLString:imageURL];
    }
}

@end
